package com.woniuxy.special.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Objects;

/**
 * 发布者发布消息时发送失败的回调
 */
public class PublishMqConfig implements RabbitTemplate.ReturnCallback, RabbitTemplate.ConfirmCallback {

    //消息投递到交换机
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            System.out.println("消息投递到交换机成功");
        } else {
            System.out.println("消息没有投递到交换机");
            //获取消息内容
            String content = new String(Objects.requireNonNull(correlationData.getReturnedMessage()).getBody(), StandardCharsets.UTF_8);
            //将消息内容存入本地消息表
        }
    }

    //消息投递到队列
    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("消息未能成功投递到队列");
        //记录消息到本地消息表
    }
}
